<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/10/11
 * Time: 8:14
 */

namespace other;


class Tree
{
    //树形图数组
    public $arr;
    //父级id字段
    public $parent_field;

    public $result;
    //树形图层级
    public $level;
    //树形结构某一级下所有子集的主键
    public $key = array();

    //生成导航链接
    public $nav;

    /**
     * Tree constructor.
     * @param array $array
     * @param string $parent_field
     */
    public function __construct($array = array(), $parent_field = 'parentid')
    {
        $this->arr = $array;
        $this->parent_field = $parent_field;
    }

    /**
     * @Author: @qubao
     * @Date: [2017-1-16]
     * @Description: [获取树形结构某一级的所有子集id]
     * @param $id 某一级的id
     * @return array
     */
    public function get_child_key($id)
    {
        if (!in_array($id, $this->key)) {
            $this->key[] = $id;
        }
        $arr = array();
        if (is_array($this->arr)) {
            foreach ($this->arr as $index => $item) {
                if ($item['parentid'] == $id) {
                    $arr[] = $item;
                }
            }
            if (!empty($arr)) {
                foreach ($arr as $val) {
                    $this->key[] = $val['id'];
                    $this->get_child_key($val['id']);
                }
            }
            return $this->key;
        }
    }

    /**
     * @Author: @qubao
     * @Date: [2017-1-14]
     * @Description: [获取树形图某级的所有子集]
     * @param $parentid 树形图父级id
     * @return array|bool
     */
    public function get_child($parentid)
    {
        $newarr = array();
        if (is_array($this->arr)) {
            foreach ($this->arr as $key => $value) {
                if ($value[$this->parent_field] == $parentid) {
                    $newarr[$key] = $value;
                }
            }
        }
        return $newarr ? $newarr : false;
    }

    /**
     * @Author: @qubao
     * @Date: [2017-1-16]
     * @Description: [获取某以及的所在所在层级]
     * @param $id 数组的主键id
     * @param int $level 参数，层级
     * @return int 从0开始  即0为1级以此类推
     */
    public function get_level($id, $level = 0)
    {
        $arr = array();
        if (is_array($this->arr)) {
            foreach ($this->arr as $index => $item) {
                $arr[$item['id']] = $item[$this->parent_field];
            }
        }
        if ($arr[$id] == 0 || $arr[$id] == '0') {
            $this->level = $level;
        } else {
            $level++;
            $this->level = $level;
            $this->get_level($arr[$id], $level);
        }
        return $this->level;
    }

    /**
     * @Author: @qubao
     * @Date: [2017-1-14]
     * @Description: [获取树形结构数组]
     * @param $parentid 父级字段id
     * @return mixed
     */
    public function get_tree($parentid)
    {
        $child = $this->get_child($parentid);
        if (is_array($child)) {
            foreach ($child as $k => $val) {
                $level = $this->get_level($val['id']);
                $val['_level'] = $level;
                $this->result[] = $val;
                $this->get_tree($val['id']);
            }
        }
        return $this->result;
    }

    public function get_tree_result($data)
    {
        $str = '└ ';
        $nbsp = '　';
        foreach ($data as $key => $value) {
            $kong = '';
            for ($i = 0; $i < $value['_level']; $i++)
            {
                $kong .= $nbsp;
            }
            if ($value['_level'] != 0)
            {
                $data[$key]['_name'] = $kong.$str.$value['cat_name'];
            } else {
                $data[$key]['_name'] = $value['cat_name'];
            }
        }
        return $data;
    }
}